package org.jzs.mybaseapp.common.utils.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;

import org.jzs.mybaseapp.R;

import java.util.concurrent.ExecutionException;

/**
 * 下载图片
 *
 * @Title: GlideUtils.java
 * @Package com.zyj.xjjjyfwpt.utils
 * @Description: TODO
 */
public class GlideUtils {

    /**
     * 加载图片(有淡入淡出)
     */
    public static void showImgAnimate(Context context, String url, ImageView iv) {
        Glide.with(context).load(url).placeholder(R.drawable.ic_default_picture)
                .error(R.drawable.ic_default_picture)
                .crossFade().into(iv);
    }


    public static void showImgLister(Context context, String url, ImageView iv, RequestListener<String, GlideDrawable> linstener) {
        Glide.with(context).load(url).placeholder(R.drawable.ic_default_picture)
                .error(R.drawable.ic_default_picture)
                .crossFade()
                .listener(linstener)
                .into(iv);
    }

    public static Bitmap getBitmapWithUrl(Context context, String url) {

        Bitmap myBitmap = null;
        try {
            myBitmap = Glide.with(context).load(url).asBitmap() // 必须
                    .centerCrop().into(600, 600).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return myBitmap;
    }

    public static Bitmap getBitmapWithUri(Context context, Uri uri) {

        Bitmap myBitmap = null;
        try {
            myBitmap = Glide.with(context).load(uri).asBitmap() // 必须
                    .centerCrop().into(600, 600).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return myBitmap;
    }


    /**
     * 加载图片(无动画)
     */
    public static void showImgNoAnimate(Context context, String url, ImageView iv) {
        Glide.with(context).load(url).placeholder(R.drawable.ic_default_picture)
                .error(R.drawable.ic_default_picture).dontAnimate().into(iv);
    }

    /**
     * 加载资源图片(无动画)
     */
    public static void showImgLocal(Context context, int url, ImageView iv) {
        Glide.with(context).load(url).placeholder(R.drawable.ic_default_picture)
                .error(R.drawable.ic_default_picture).dontAnimate().into(iv);
    }


    /**
     * 无动画(设置占位图)
     */
    public static void showImgSimpleByPlaceholder(Context context, String url, ImageView iv, int placeholderImg) {
        showImgSimple(context, url, iv, 0, false, placeholderImg, R.drawable.ic_default_picture, Bitmap.Config.ARGB_8888);
    }

    /**
     * 无动画
     *
     * @param angle 圆角角度
     */
    public static void showImgSimple(Context context, String url, ImageView iv, int angle) {

        showImgSimple(context, url, iv, angle, false, R.drawable.ic_default_picture, R.drawable.ic_default_picture, Bitmap.Config.ARGB_8888);
    }


    /**
     * @param context 上下文
     * @param url     图片URL
     * @param iv      imageView
     * @param angle   圆角角度
     * @param isAnim  是否开启动画(默认开启)
     * @param config  图片类型,默认为ARGB_8888（ARGB_8888、ARGB_4444等）
     * @return void
     * @Title: showImgSimple
     * @Description: 圆角图片
     */
    public static void showImgSimple(Context context, String url, ImageView iv, int angle, boolean isAnim, int placeholderImg, int failImg, Config config) {

        if (context == null) {
            return;
        }

        if (isAnim) {
            if (angle == 90) {
                Glide.with(context).load(url).transform(new GlideCircleTransform(context, config)).placeholder(placeholderImg)
                        .error(failImg).crossFade().into(iv);
            } else {
                Glide.with(context).load(url).transform(new GlideRoundTransform(context, angle, config)).placeholder(placeholderImg)
                        .error(failImg).crossFade().into(iv);
            }

        } else {
            if (angle == 90) {
                Glide.with(context).load(url).transform(new GlideCircleTransform(context, config)).placeholder(placeholderImg)
                        .error(failImg).dontAnimate().into(iv);
            } else {
                Glide.with(context).load(url).transform(new GlideRoundTransform(context, angle, config)).placeholder(placeholderImg)
                        .error(failImg).dontAnimate().into(iv);
            }
        }
    }


}
